<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/
?>
<?php
define( "AT_MOVIE_PATH", "/movies");
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
    protected function _initAutoload(){
        $autoloader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Default',
            'basePath'  => dirname(__FILE__),
        ));

        return $autoloader;
    }

    protected function _initView()
    {
		$theme = 'default';

		$options = $this->getOptions();

		if( isset( $options['resources']['layout'] ) && isset( $options['resources']['layout']['theme'] ) ){
			$theme = $options['resources']['layout']['theme'];
		}


        $path = str_replace( 'application', 'public', APPLICATION_PATH ) . '/themes/' . $theme;

		// force layoutpath to theme directory
        $layout = Zend_Layout::startMvc()
            ->setLayout('layout')
            ->setLayoutPath( $path )
            ->setContentKey('content');

        // Initialize view
        $view = new Zend_View();
        $view->doctype('XHTML1_STRICT');
        $view->headTitle('Annotrax')->setSeparator(' - ');

		// Initialize translate. use a simple array for now
		$view->getHelper( 'translate' )->setTranslator(
			new Zend_Translate( 'array', array('color' => 'colour'), 'en-US')
		);

		// temporarily include application/views and initialize the themeUrl handler
		$view->setBasePath( APPLICATION_PATH . '/views/' );
		$view->getHelper( 'themeUrl' )->setTheme( $theme );

		// allow helpers in theme
		$view->setBasePath( $path );

		// set scriptpath to theme directory
		$view->setScriptPath( $path . '/templates' );

        // Add it to the ViewRenderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );

        $viewRenderer->setView($view);

	    // Return it, so that it can be stored by the bootstrap
        return $view;
    }

    protected function _initHelpers(){
		$options = $this->getOption('resources');

		$locale    = isset( $options['frontController']['locale'] ) ? $options['frontController']['locale'] : 'en_US';
		$timezone  = isset( $options['frontController']['timezone'] ) ? $options['frontController']['timezone'] : 'europe/amsterdam';

/*
public function initLocale() {
    $localeValue = 'en';

    $locale = new Zend_Locale($localeValue);
    Zend_Registry::set('Zend_Locale', $locale);

    $translationFile = $this->_root . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $localeValue . '.inc.php';

    $translate = new Zend_Translate('array', $translationFile, $localeValue);
    Zend_Registry::set('Zend_Translate', $translate);
}
*/
		setlocale( LC_ALL, $locale );
		Zend_Locale::setDefault( $locale );
		date_default_timezone_set( $timezone);

		$translate = new Zend_Translate( 'array', array('foo'=>'foo'), $locale );
		Zend_Registry::set( 'Zend_Translate', $translate );

		// expose site-wide configuration options to registry
		// @todo this should initialize the view helper topmovies
		$atConfig = new Zend_Config_Ini( APPLICATION_PATH . '/configs/annotrax.ini');

		Zend_Registry::set( 'atConfig', $atConfig );

		$frontController = Zend_Controller_Front::getInstance();

		// initialize support for modules: admin
		$frontController->addModuleDirectory(APPLICATION_PATH.'/modules');

		$modules = $frontController->getControllerDirectory();

		foreach($modules as $name => $path) {
		   $autoloader = new Zend_Application_Module_Autoloader( array(
			  'namespace' => ucfirst($name).'_',
			  'basePath'  => APPLICATION_PATH . "/modules/$name"
			));
		  Zend_Controller_Action_HelperBroker::addPath($path, ucfirst($name) . '_Helper');
		}

        // enable action helpers in application/helpers
        Zend_Controller_Action_HelperBroker::addPath(
                APPLICATION_PATH .'/helpers', 'Annotrax_Helper');

   }

}
